package com.neu.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.neu.dao.impl.WorkerDao;
import com.neu.pojo.Worker;
import com.neu.service.impl.WorkerService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

public class RegistUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	
	private WorkerService workerService = WorkerService.getInstance();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegistUI frame = new RegistUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RegistUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 468, 444);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("账号	");
		lblNewLabel.setBounds(97, 25, 58, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("密码");
		lblNewLabel_1.setBounds(97, 63, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("姓名");
		lblNewLabel_2.setBounds(97, 102, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("性别");
		lblNewLabel_3.setBounds(97, 154, 58, 15);
		contentPane.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("生日");
		lblNewLabel_4.setBounds(97, 199, 58, 15);
		contentPane.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("专长");
		lblNewLabel_5.setBounds(97, 243, 58, 15);
		contentPane.add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("职称");
		lblNewLabel_6.setBounds(97, 287, 58, 15);
		contentPane.add(lblNewLabel_6);
		
		JLabel lblNewLabel_7 = new JLabel("电话");
		lblNewLabel_7.setBounds(97, 331, 58, 15);
		contentPane.add(lblNewLabel_7);
		
		textField = new JTextField();
		textField.setBounds(263, 22, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(263, 60, 66, 21);
		contentPane.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(263, 99, 66, 21);
		contentPane.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(263, 151, 66, 21);
		contentPane.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(263, 196, 66, 21);
		contentPane.add(textField_4);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(263, 240, 66, 21);
		contentPane.add(textField_5);
		
		textField_6 = new JTextField();
		textField_6.setColumns(10);
		textField_6.setBounds(263, 284, 66, 21);
		contentPane.add(textField_6);
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		textField_7.setBounds(263, 328, 66, 21);
		contentPane.add(textField_7);
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.setBounds(83, 374, 97, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.setBounds(256, 374, 97, 23);
		contentPane.add(btnNewButton_1);
		
		//窗口居中
		setLocationRelativeTo(null);
		
		//取消
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				AdminUI adminUI = new AdminUI();//跳转新窗口
				adminUI.setVisible(true);
			}
		});
		
		//添加
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//注册，添加worker
				if(textField.getText().length() != 0&&textField_1.getText().length() != 0&&textField_2.getText().length() != 0&&textField_3.getText().length() != 0&&textField_4.getText().length() != 0&&textField_5.getText().length() != 0&&textField_6.getText().length() != 0&&textField_7.getText().length() != 0) {
					if(workerService.getInstance().add(new Worker(textField.getText(), textField_1.getText(),textField_2.getText(), textField_4.getText(), textField_5.getText(), textField_6.getText(),textField_7.getText()))) {
						JOptionPane.showMessageDialog(null, "注册成功");//提示
						AdminUI adminUI = new AdminUI();//跳转新窗口
						adminUI.setVisible(true);
					}else {
						JOptionPane.showMessageDialog(null, "注册失败");//提示
						AdminUI adminUI = new AdminUI();//跳转新窗口
						adminUI.setVisible(true);
					}
					setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null, "请输入信息");//提示
				}
			}
		});
	}
}
